﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MongoDB.Bson;
using MongoDB.Driver;
using System.Security;
using System.IO;
using System.Web.Hosting;
using System.Web.Http.Description;
using System.Web.UI.WebControls;
using SignatureWebApi.Models;
#pragma warning disable 1591

namespace SignatureWebApi.Controllers
{

    public class User
    {
        public ObjectId Id { get; set; }
        public string UserName { get; set; }
        public string UserRole { get; set; }
        public byte[] pic { get; set; }
    }
    //[Authorize]
    [ApiExplorerSettings(IgnoreApi = true)]
    [Authorize(Roles = "AuthenticatedUser")]
    public class MongoDbController : ApiController
    {
        // GET: api/MongoDb
        //public string Get()
        //{
        //    //MongoDbInstance MongoDB = new MongoDbInstance("localhost", 27017, "root", "81700214", "admin");
        //    var MongoDB = MongoDbInstanceStore.Client1;
        //    var client = MongoDB.Client;
        //    //var client = new MongoClient(mongoClientSettings);
        //    var database = client.GetDatabase("Users");
        //    //var collection = database.GetCollection<User>("UserData");
        //    var collection = database.GetCollection<BsonDocument>("UserData");
        //    var mappedPath = HostingEnvironment.MapPath("~/");

        //    FileStream picStream = new FileStream(mappedPath + "App_Data/Test.jpg", FileMode.Open);
        //    byte[] PicBuf = new byte[picStream.Length];
        //    picStream.Read(PicBuf, 0, PicBuf.Length);
        //    picStream.Close();
        //    //var userDocument = new BsonDocument(new Dictionary<string, object>()
        //    //{
        //    //    {"UserName", "zhuanshicong"},
        //    //    {"UserRole", "normal"},
        //    //    {"pic", PicBuf}
        //    //});
        //    var filter = new BsonDocument("UserName", "zhuanshicong3");
        //    ////var update = Builders<BsonDocument>.Update.Set("UserName", "zhuanshicong");
        //    //collection.InsertOne(userDocument);
        //    //var userDocument2 = new BsonDocument(new Dictionary<string, object>()
        //    //{
        //    //    {"UserName", "zhuanshicong"},
        //    //    {"UserRole", "admin"},
        //    //    {"pic", PicBuf}
        //    //});
        //    var userDocument3 = new BsonDocument(new Dictionary<string, object>()
        //    {
        //        {"UserName", "zhuanshicong"}
        //    });
        //    //collection.FindOneAndReplace(filter, userDocument2);
        //    //var list =collection.Find(new BsonDocument("UserName", "zhuanshicong")).ToList();
        //    //string re = "";
        //    //foreach (var person in list)
        //    //{
        //    //    re = person["UserName"].ToString();
        //    //}
        //    //var filter = new BsonDocument("UserName", "zhuanshicong");
        //    //var result = collection.DeleteOne(filter).DeletedCount.ToString();
        //    //collection.InsertOne(userDocument2);
        //    //var update = new BsonDocument("$set", userDocument3);

        //    //var result = collection.Find(userDocument3).ToCursor();
        //    List<Dictionary<string, string>> rows = null;
        //    using (var cursor = collection.Find(userDocument3).ToCursor())
        //    {
                
        //        rows = new List<Dictionary<string, string>>();
        //        while (cursor.MoveNext())
        //        {
                    
        //            //for (var i = 0; i < reader.FieldCount; i++)
        //            //{
        //            //    var columnName = reader.GetName(i);
        //            //    var columnValue = reader.IsDBNull(i) ? null : reader.GetString(i);
        //            //    row.Add(columnName, columnValue);
        //            //}
        //            foreach (var document in cursor.Current)
        //            {
        //                var row = new Dictionary<string, string>();
        //                foreach (var element in document)
        //                {
        //                    var columnName = element.Name;
        //                    var columnValue = element.Value.ToString();
        //                    row.Add(columnName, columnValue);
        //                }

        //                rows.Add(row);
        //            }
        //        }
        //    }

        //    var tt = rows;
        //    string ttt = rows.ToJson();
        //    //foreach (var data in result)
        //    //{
        //    //    var test = data["UserBool"];
        //    //    ttt = data["UserBool"].ToString();
                
        //    //}
        //    return ttt;
        //}

        // GET: api/MongoDb/5
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/MongoDb
        public string Post(SignatureCreateModel model)
        {
            //MongoDbInstance mongoDb = MongoDbInstanceStore.DataStoreInstance;
            //var addDocument = model.ToBsonDocument();
            //var createResult = mongoDb.InsertSingleDocument("DataStore", "SignatureStore", addDocument);
            return "Success";
        }

        // PUT: api/MongoDb/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/MongoDb/5
        public void Delete(int id)
        {
        }
    }
}
